package com.linoer.cloud.alo.common.domain.exception;

import com.linoer.cloud.alo.common.domain.ObjectResponse;
import com.linoer.cloud.alo.common.domain.enums.ResponseStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Classname GlobalExceptionHandler
 * @Description TODO
 * @Date 2021/6/22 8:43 下午
 * @Created by muhenglv
 */
@ControllerAdvice("com.linoer.cloud.alo")
public class GlobalExceptionHandler {
    private static final Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ObjectResponse exceptionHandler(Exception e){
        LOGGER.error("【系统抛出Exception异常】 —— 异常内容如下：{}" , e.toString());
        ObjectResponse objectResponse = new ObjectResponse<>();
        objectResponse.setStatus(ResponseStatus.FAIL.getCode());
        objectResponse.setMessage(ResponseStatus.FAIL.getMessage());
        return objectResponse;
    }

    @ExceptionHandler(DefaultException.class)
    @ResponseBody
    public ObjectResponse defaultException(DefaultException e){
        LOGGER.error("【系统抛出ALOException异常】 —— 异常内容如下：{}" , e.toString());
        ObjectResponse objectResponse = new ObjectResponse<>();
        objectResponse.setStatus(ResponseStatus.FAIL.getCode());
        objectResponse.setMessage(ResponseStatus.FAIL.getMessage());
        return objectResponse;
    }
}
